 //
//  EmotionManager.swift
//  EmotionKeyboard
//
//  Created by 史亚洲 on 16/1/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class EmotionManager: NSObject {
    
    //分组表情的数组
    lazy var packages=[EmotionPackages]()
    
    
     override init() {
        
        super.init()
        self.loadEmotion()
    }
    
    //1.加载bundle路径下的表情
    func loadEmotion() {
        //1.1获取bundle路径下的emtion.plist文件
        let path=NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil,inDirectory: "Emoticons.bundle")
        guard let filePath=path else {
            print("文件路径不存在")
            return
            
        }
        
        //1.2将路径下的文件转换为字典
        let dict=NSDictionary(contentsOfFile: filePath)!
        //print(dict)
        
     
        
        //1.3获取packages对应的数组
        let array=dict["packages"] as! [[String: AnyObject]]
        print(array)
        
        //1.4遍历数组,获取文件的id从而找到分组表情文件夹
        for item in array {
            
            let id=item["id"] as! String
            
            //print(id)
            loadGroupEmotion(id)
        }
    }

    //2.加载分组的表情
    private func loadGroupEmotion(id: String) {
        
        //2.1获取info.plist文件
        let path=NSBundle.mainBundle().pathForResource("Info.plist", ofType: nil, inDirectory: "Emoticons.bundle/"+"\(id)")
        guard let filePath=path else {
            print("文件路径不存在")
            return
        }
        
        //2.2获取分组表情的info.plist文件
        let dict=NSDictionary(contentsOfFile: filePath)!
        
        print(dict)
        
        
        
        //2.3获取大的分组模型
        let group_name_cn=dict["group_name_cn"] as! String
        let emoticonArray=dict["emoticons"] as! [[String: String]]
        
        //2.4实例化分组表情模型
        let p = EmotionPackages(id: id, group_name_cn: group_name_cn, emotionArray: emoticonArray)
        
        //添加到数组
        packages.append(p)

        
    }
    


}
